package com.dangdang.web;

import com.dangdang.pojo.Book;
import com.dangdang.pojo.Cart;
import com.dangdang.pojo.CartItem;
import com.dangdang.service.BookService;
import com.dangdang.service.impl.BookServiceImpl;
import com.dangdang.utils.WebUtils;
import com.google.gson.Gson;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class CartServlet extends BaseServlet {
    private BookService bookService = new BookServiceImpl();

    /**
     * 加入购物车
     *
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void ajaxAddItem(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        // 获取请求的参数 商品编号
        int id = WebUtils.parseInt(req.getParameter("id"), 0);
        // 调用 bookService.queryBookById(id):Book 得到图书的信息
        Book book = bookService.queryBookById(id);
        // 返回信息
        Map<String, Object> resultMap = new HashMap<>();

        int count = 0;

        if (req.getSession().getAttribute(Integer.toString(id)) != null) {
            count = (int) req.getSession().getAttribute(Integer.toString(id));
        }

        if (count >= book.getStock()) {
            resultMap.put("lastName", book.getName());
            resultMap.put("result", false);
            req.getSession().setAttribute("flag", false);
        } else {
            // 保存已加入的购物车每本书的数量
            req.getSession().setAttribute(Integer.toString(id), count+1);

            // 把图书信息，转换成为 CartItem 商品项
            CartItem cartItem = new CartItem(book.getId(), book.getName(), 1, book.getPrice(), book.getPrice());
            // 调用 Cart.addItem(CartItem);添加商品项
            Cart cart = (Cart) req.getSession().getAttribute("cart");
            if (cart == null) {
                cart = new Cart();
                req.getSession().setAttribute("cart", cart);
            }
            cart.addItem(cartItem);

            // 最后一个添加的商品名称
            req.getSession().setAttribute("lastName", cartItem.getName());
            req.getSession().setAttribute("flag", true);

            // 返回购物车总的商品数量和最后一个添加的商品名称
            resultMap.put("totalCount", cart.getTotalCount());
            resultMap.put("lastName", cartItem.getName());
            resultMap.put("result", true);
        }

        Gson gson = new Gson();
        String toJson = gson.toJson(resultMap);

        resp.getWriter().write(toJson);
    }

    /**
     * 删除购物车
     *
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void deleteItem(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        // 获取请求的参数 商品编号
        int id = WebUtils.parseInt(req.getParameter("id"), 0);
        // 获取购物车对象
        Cart cart = (Cart) req.getSession().getAttribute("cart");

        if (cart != null) {
            // 删除了购物车商品项
            cart.deleteItem(id);

            req.getSession().setAttribute(Integer.toString(id), 0);

            // 重定向回原来商品所在的地址页面
            resp.sendRedirect(req.getHeader("Referer"));
        }
    }

    /**
     * 清空购物车
     *
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void clear(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 获取购物车对象
        Cart cart = (Cart) req.getSession().getAttribute("cart");

        if (cart != null) {
            // 清零购物车中的所有书数量
            Map<Integer, CartItem> ids = cart.getItems();
            for (Integer id : ids.keySet()) {
                req.getSession().setAttribute(Integer.toString(id), 0);
            }

            // 清空购物车
            cart.clear();

            // 重定向回原来商品所在的地址页面
            resp.sendRedirect(req.getHeader("Referer"));
        }
    }

    /**
     * 修改商品数量
     *
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void updateCount(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 获取请求的参数 商品编号、商品数量
        int id = WebUtils.parseInt(req.getParameter("id"), 0);
        int count = WebUtils.parseInt(req.getParameter("count"), 1);
        // 获取购物车对象
        Cart cart = (Cart) req.getSession().getAttribute("cart");

        if (cart != null) {
            //修改商品数量
            cart.updateCount(id, count);

            req.getSession().setAttribute(Integer.toString(id), count);

            // 重定向回原来商品所在的地址页面
            resp.sendRedirect(req.getHeader("Referer"));
        }

    }

    /**
     * 获取商品数量
     *
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void getCount(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        int id = WebUtils.parseInt(req.getParameter("id"), 0);

        Map<String, Object> resultMap = new HashMap<>();

        Book book = bookService.queryBookById(id);

        resultMap.put("stock", book.getStock());

        Gson gson = new Gson();
        String toJson = gson.toJson(resultMap);

        resp.getWriter().write(toJson);
    }
    /**
     * 扫码付款
     *
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void getPayPage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.getRequestDispatcher("/pages/order/pay.jsp").forward(req,resp);
    }



}
